package Month4;

import java.io.File;

public class a20250424FileTest4
{
    //删除多级文件夹 先删掉文件 再删掉自己 如果有文件夹用递归
    public static void main(String[] args)
    {
        File src = new File("myfile");
        try
        {
            seeAllFiles(src);
            deleteAll(src);
            seeAllFiles(src);
        }
        catch(NullPointerException e)
        {
            System.out.println(e.getMessage());
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    public static void seeAllFiles(File f)
    {

        File[] files = f.listFiles();
        if(files == null)
        {
            throw  new NullPointerException();
        }
        for(File temp : files)
        {
            System.out.println(temp.getName());
        }
    }

    public static void deleteAll(File f)
    {
        //System.out.println("+++++++"+ f.getName());
        File[] files = f.listFiles();
        for(File temp : files)
        {
            if(files == null)
            {
                throw  new NullPointerException();
            }
            if(temp.isFile())
            {
                temp.delete();
            }
            else
            {
                deleteAll(temp);
            }
        }
        f.delete();
    }
}
